package com.woniuxy.creditsystem.productmag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.PageRequest;
import com.woniuxy.creditsystem.common.bean.ProductInfo;
import com.woniuxy.creditsystem.common.bean.ResultMsg;
import com.woniuxy.creditsystem.common.bean.UserInfo;
import com.woniuxy.creditsystem.common.mapper.ProductMapper;
import com.woniuxy.creditsystem.productmag.service.IProduct;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * 合作产品 业务实现类 夏先生
 * */
@Transactional(readOnly = true)
@Service
public class IProductImpl implements IProduct {

    @Resource
    private ProductMapper productMapper;


    /**
     * 分页查询
     * @param request
     * @param productInfo
     * @return
     */
    @Override
    public PageInfo<ProductInfo> findAllByParams(PageRequest request, ProductInfo productInfo) {
        return PageHelper.startPage(request.getPageNum(),request.getLimit()).
                doSelectPageInfo(()->productMapper.findAllByParams(request,productInfo));
    }
    /**
     * 修改管理员
     * @author hejie
     * @param productInfo
     * @return
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int updaProductInfo(ProductInfo productInfo) {
        return productMapper.updaProductInfo(productInfo);
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int updaStatusById(ProductInfo productInfo) {
        return productMapper.updaStatusById(productInfo);
    }

    @Override
    public List<ProductInfo> findProductByDitchId(ProductInfo productInfo) {
        return productMapper.findProductByDitchId(productInfo);
    }


    @Override
    public ResultMsg write2excel(List<ProductInfo> tableDataJson, String path) throws IOException {
        if (tableDataJson==null){
            return new ResultMsg(201,"页面表格内容为空，没有数据可以导出！",null);
        }


        //创建表对象，即工作簿对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //第一个sheet对象demo1
        HSSFSheet sheet = workbook.createSheet("demo1");
        //行对象
        HSSFRow row = sheet.createRow(0);
        //单元格对象
        HSSFCell cell0 = row.createCell(0);
        cell0.setCellValue("产品编号");
        HSSFCell cell1 = row.createCell(1);
        cell1.setCellValue("产品名称");
        HSSFCell cell2 = row.createCell(2);
        cell2.setCellValue("状态");
        HSSFCell cell3 = row.createCell(3);
        cell3.setCellValue("渠道数");
        HSSFCell cell4 = row.createCell(4);
        cell4.setCellValue("最后更新人");
        HSSFCell cell5 = row.createCell(5);
        cell5.setCellValue("创建时间");

        //解决日期显示不正常的问题：给第1行、第2列单元格写入日期
        //用Excel表对象获取CreationHelper对象
        CreationHelper createHelper = workbook.getCreationHelper();
        //用Excel表对象创建CellStyle对象
        CellStyle cellStyle = workbook.createCellStyle();
        //给cellStyle对象设置Date格式
        cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));

        for (int i=0; i<tableDataJson.size(); i++){
            //行对象
            HSSFRow r = sheet.createRow(1+i);
            for (int j=0; j<6; j++){
                //单元格对象
                HSSFCell cellProductNums = r.createCell(0);
                cellProductNums.setCellValue(tableDataJson.get(i).getProductNums());
                HSSFCell cellProductName = r.createCell(1);
                cellProductName.setCellValue(tableDataJson.get(i).getProductName());
                HSSFCell cellProductStatus = r.createCell(2);
                cellProductStatus.setCellValue(tableDataJson.get(i).getProductStatus());


                HSSFCell cellDitchNums = r.createCell(3);
                cellDitchNums.setCellValue(tableDataJson.get(i).getDitchNums());

                HSSFCell cellEndPerson = r.createCell(4);
                cellEndPerson.setCellValue(tableDataJson.get(i).getEndPerson());
                HSSFCell cellCreateTime = r.createCell(5);
                //设置 “Date”类型的显示格式
                cellCreateTime.setCellStyle(cellStyle);
                cellCreateTime.setCellValue(tableDataJson.get(i).getCreateTime());
            }
        }

        FileOutputStream os = null;
        String filePathName = path + File.separator + "产品管理表" + System.currentTimeMillis() + ".xls";
        File file=new File(path);
        if (!file.exists()){
            file.mkdirs();
        }
        try {
            os = new FileOutputStream(filePathName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //		workbook1.write(os);
        try {
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        os.flush();
        os.close();

        ResultMsg resultMsg=  new ResultMsg(200,"页面表格数据已经写入到"+filePathName,null);
        return resultMsg;
    }


    /**
     *   产品的增加
     * @param productInfo
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public int savaProductInfo(ProductInfo productInfo) {

        return productMapper.saveProductInfo(productInfo);
    }




}
